﻿//===============================================================================
// File: SpecialtyHelper.cs
// Author:Veiller Hu
// CLR Version：4.0.30319.1
// Created Date:  2011/2/7 3:49:43 PM 
// Summary:  
//===============================================================================
//
// Copyright (c) East China Information Technology Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GPMS.Model;

namespace GPMS.BLL
{
    public class SpecialtyHelper
    {
        /// <summary>
        /// 判断对应名称是否存在
        /// </summary>
        /// <param name="specialtyName"></param>
        /// <param name="gpms"></param>
        /// <returns></returns>
        public static bool IsExist(string specialtyName,ExtendedGPMSDb gpms)
        {
            return gpms.Specialties.Any(s => s.SpecialtyName == specialtyName && s.IsDeleted == false);
        }

        /// <summary>
        /// 获得专业entity
        /// </summary>
        /// <param name="id"></param>
        /// <param name="gpms"></param>
        /// <returns></returns>
        public static Specialty Get(Guid id, ExtendedGPMSDb gpms)
        {
            return gpms.Specialties.FirstOrDefault(s => s.SpecialtyID == id && s.IsDeleted == false);
        }

        /// <summary>
        /// 新增专业
        /// </summary>
        /// <param name="specialty"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static Guid Add(Specialty specialty, ExtendedGPMSDb db)
        {
            if (specialty.SpecialtyID==Guid.Empty)
            {
                specialty.SpecialtyID = Guid.NewGuid();
            }
            if (IsExist(specialty.SpecialtyName,db))
            {
                throw new DuplicateException();
            }
            specialty.CreateDate = DateTime.Now;
            specialty.IsDeleted = false;
            db.Specialties.AddObject(specialty);
            db.SaveChanges();
            return specialty.SpecialtyID;
        }

        /// <summary>
        /// 删除专业
        /// </summary>
        /// <param name="id"></param>
        /// <param name="db"></param>
        public static void Delete(Guid id, ExtendedGPMSDb db)
        {
            Specialty specialty = db.Specialties.FirstOrDefault(s => s.SpecialtyID == id);
            specialty.IsDeleted = true;
            db.SaveChanges();
        }
    }
}
